package com.myfoon.utils;


import java.sql.Date;
import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.ResourceBundle;


public class CalendarUtils {
    public CalendarUtils() {
    }

    public static Date createDate(int year, int month, int day) {
        Calendar c = new GregorianCalendar();
        c.set(year, month - 1, day, 0, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        return new Date(c.getTime().getTime());
    }
/*
    public static List<SelectItem> getYearList() {
        List<SelectItem> yearList = new ArrayList<SelectItem>();

        for (int i = 2000; i > 1908; i--) {
            yearList.add(new SelectItem(i, String.valueOf(i)));
        }

        return yearList;
    }

    public static List<SelectItem> getMonthList() {
        List<SelectItem> monthList = new ArrayList<SelectItem>();

        FacesContext context = FacesContext.getCurrentInstance();
        Application application = context.getApplication();
        ResourceBundle bundle =
            application.getResourceBundle(context, "messages");

        for (int i = 1; i < 13; i++) {
            monthList.add(new SelectItem(i - 1,
                                         bundle.getString("global.month." +
                                                          String.valueOf(i))));
        }

        return monthList;
    }

    public static List<SelectItem> getDayList() {
        List<SelectItem> dayList = new ArrayList<SelectItem>();

        for (int i = 1; i < 32; i++) {
            dayList.add(new SelectItem(i - 1, String.valueOf(i)));
        }

        return dayList;
    }

    public static List<SelectItem> getHourList() {
        List<SelectItem> hourList = new ArrayList<SelectItem>();

        for (int i = 1; i < 24; i++) {
            hourList.add(new SelectItem(i - 1, String.valueOf(i)));
        }

        return hourList;
    }

    public static List<SelectItem> getMinuteList() {
        List<SelectItem> minuteList = new ArrayList<SelectItem>();
        minuteList.add(new SelectItem(0, "00"));
        minuteList.add(new SelectItem(1, "15"));
        minuteList.add(new SelectItem(2, "30"));
        minuteList.add(new SelectItem(3, "45"));

        return minuteList;
    }
*/
    public Timestamp createDate(String date_select, 
                                String date_year, String date_month,
                                String date_day, String time_hour,
                                String time_minute, Integer TimezoneOffset) {
                
        Calendar cal = Calendar.getInstance();              
        int day = cal.get(Calendar.DATE);
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);   
        int hour = cal.get(Calendar.HOUR);
        int minute = cal.get(Calendar.MINUTE);
        
        cal.add(Calendar.MILLISECOND,-cal.get(Calendar.DST_OFFSET) - cal.get(Calendar.ZONE_OFFSET));
        
        int ds = Integer.valueOf(date_select);      
        int dd = 0; /* Day Difference */        
        switch (ds) {
            case 0 : dd = -2; break;
            case 1 : dd = -1; break;            
            case 4 : dd = +1; break;
            case 5 : dd = +2; break;
            default: dd = 0;
        }        
        
        if (ds < 6 && ds != 3) {                                
            cal.set(year, month, day+dd, Integer.valueOf(time_hour) +TimezoneOffset, Integer.valueOf(time_minute)*15, 0);
            return new Timestamp(cal.getTimeInMillis());                                
        }        
        else if (ds == 6) {            
            cal.set(Integer.valueOf(date_year), Integer.valueOf(date_month)-1, Integer.valueOf(date_day), Integer.valueOf(time_hour) +TimezoneOffset, Integer.valueOf(time_minute)*15, 0);
            return new Timestamp(cal.getTimeInMillis());                                
        }
        else {
            //cal.set(year, month, day, hour + TimezoneOffset, minute, 0);
            //return new Timestamp(cal.getTimeInMillis());                
            System.out.println("Buradayım!");                    
            return new Timestamp(cal.getTimeInMillis());
        }                      
    }

}
